﻿using System;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class BaseContentType : IContentType<BaseContentTypeDto>
    {
        public static ContentTypeIdFieldType ContentTypeId
        {
            get { return new ContentTypeIdFieldType(SPBuiltInFieldId.ContentTypeId); }
        }

        public static IntegerFieldType Id
        {
            get { return new IntegerFieldType(SPBuiltInFieldId.ID); }
        }

        public static DateTimeFieldType Modified
        {
            get { return new DateTimeFieldType(SPBuiltInFieldId.Modified); }
        }

        public static SingleUserFieldType ModifiedBy
        {
            get { return new SingleUserFieldType(SPBuiltInFieldId.Editor); }
        }

        public static DateTimeFieldType Created
        {
            get { return new DateTimeFieldType(SPBuiltInFieldId.Created); }
        }

        public static SingleUserFieldType CreatedBy
        {
            get { return new SingleUserFieldType(SPBuiltInFieldId.Author); }
        }

        public BaseContentTypeDto Get(object item)
        {
            var spListItem = item as SPListItem;
            var contentTypeId = ContentTypeId.GetValue(spListItem);
            var id = Id.GetValue(spListItem);
            var createdBy = CreatedBy.GetValue(spListItem);
            var created = Created.GetValue(spListItem);
            var modifiedBy = ModifiedBy.GetValue(spListItem);
            var modified = Modified.GetValue(spListItem);

            return new BaseContentTypeDto(contentTypeId, id, createdBy, created, modifiedBy, modified);
        }

        public void Update(object item, BaseContentTypeDto dto)
        {
            var spListItem = item as SPListItem;

            //ContentTypeId.SetValue(spListItem, dto.ContentTypeId);
            Id.SetValue(spListItem, dto.Id);
            CreatedBy.SetValue(spListItem, dto.CreatedBy);
            Created.SetValue(spListItem, dto.Created);
            ModifiedBy.SetValue(spListItem, dto.ModifiedBy);
            ModifiedBy.SetValue(spListItem, dto.ModifiedBy);
        }

        public virtual string GetContentTypeIdDefinition()
        {
            return SPBuiltInContentTypeId.System.ToString();
        }
    }
}